package org.neuedu.his.service;

import org.neuedu.his.mapper.UserMapper;
import org.neuedu.his.model.Role;
import org.neuedu.his.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserService implements UserDetailsService {
    @Autowired
    UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        User user=userMapper.loadUserByUsername(s);
        if (user==null){
            throw new UsernameNotFoundException("用户名不存在");
        }
        List<Role> roles=userMapper.getRolesByID(user.getId());
        user.setRoles(roles);
        return user;
    }
}
